/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tarena.lbs.attach.rpc;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.dao.mapper.AttachMapper;
import com.tarena.lbs.attach.dao.repository.AttachRepository;
import com.tarena.lbs.attach.enums.AttachResultEnum;
import com.tarena.lbs.attach.enums.FileTypeEnum;
import com.tarena.lbs.attach.pojo.dto.AttachDTO;
import com.tarena.lbs.attach.pojo.dto.AttachQrDTO;
import com.tarena.lbs.attach.pojo.param.AttachQRParam;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.attach.pojo.po.AttachPO;
import com.tarena.lbs.attach.pojo.query.AttachQuery;
import com.tarena.lbs.attach.service.AttachService;
import com.tarena.lbs.attach.utils.FileUtil;
import com.tarena.lbs.attach.utils.QRCodeUtil;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.enums.BusinessTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

@Service
@DubboService
@Slf4j
public class AttachRpcService implements AttachApi {

    @Autowired
    private AttachRepository attachRepository;
    @Autowired
    private AttachService attachService;

    @Override
    public List<AttachDTO> getAttachInfo(AttachQuery attachQuery) {
        List<AttachDTO> attachDTOList = attachRepository.findAttachInfo(attachQuery);
        return attachDTOList;
    }

    @Override
    public boolean batchUpdateBusiness(List<PicUpdateParam> picParams) {
        log.info("绑定图片 执行sql之前:{}",picParams);
        int rows=attachRepository.batchUpdateAttachByIdOrFileUUID(picParams);
        log.info("绑定图片 执行sql之后:{}",rows);
        return rows>0;
    }

    @Override
    public boolean deleteBusinessAttach(AttachQuery attachQuery) {

        //删除数据库记录
        log.info("删除业务关联的图片,入参:{}",attachQuery);
        int rows=attachRepository.deleteAttachByBusinessIdAndType(attachQuery);
        log.info("删除业务关联的图片,执行sql之后:{}",rows);
        return rows>0;
    }

    @Override
    public int deleteUnBindAttach() {
        //获得图片信息
        AttachQuery attachQuery=new AttachQuery();
        attachQuery.setBusinessType(0);
        attachQuery.setBusinessId(0);

        log.info("查询无业务关联的图片,入参:{}",attachQuery);
        List<AttachDTO> attachDTOList = attachRepository.findAttachInfo(attachQuery);
        log.info("查询无业务关联的图片,出参:{}",attachDTOList);
        if(attachDTOList.isEmpty()){
            log.info("未查询到无业务关联的图片信息");
            return 0;
        }
        //获得图片url列表
        List<String> fileUrlList = new ArrayList<>();
        for (AttachDTO attachDTO : attachDTOList) {
            log.info("图片信息:{}",attachDTO);
            String fileUrl = imagePath+attachDTO.getFileUuid();
            log.info("图片url:{}",fileUrl);
            fileUrlList.add(fileUrl);
        }
        log.info("图片url列表:{}",fileUrlList);
        //删除数据库记录
        int rows=attachRepository.deleteUnBindAttach(attachQuery);
        log.info("删除无业务关联的图片,执行sql之后:{}",rows);
        //删除图片文件
        FileUtil.deleteFile(fileUrlList);
        return rows;
    }

    @Value("${image_path}")
    private String imagePath;
    @Value("${url}")
    private String url;
    @Override
    public AttachQrDTO generateQrCode(AttachQRParam attachQRParam) {
        log.info("生成二维码参数:{}",attachQRParam);
        String urlPath = "";
        String fileUuid = UUID.randomUUID().toString().replace("-", "").toUpperCase(Locale.ROOT);
        String originalFilename = "二维码"+attachQRParam.getBusinessId()+".png";
        String fileName = fileUuid+originalFilename;
        urlPath = url + fileName;
        File qrCodePath = new File(imagePath);
        //生成二维码
        QRCodeUtil.createCodeToFile(attachQRParam.getContent(),qrCodePath,fileName);
        //保存二维码信息

        AttachPO attachPO = new AttachPO();
        attachPO.setClientFileName(originalFilename);
        attachPO.setContentType(FileTypeEnum.PICTURE.getType());
        attachPO.setFileUuid(fileUuid);
        attachPO.setBusinessId(attachQRParam.getBusinessId());
        attachPO.setBusinessType(attachQRParam.getBusinessType());
        //存入数据库
        AttachDTO attachDTO = attachRepository.save(attachPO);
        AttachQrDTO attachQrDTO = new AttachQrDTO();
        attachQrDTO.setFileUuid(fileUuid);
        attachQrDTO.setUrl(urlPath);
        attachQrDTO.setId(attachDTO.getId());
        return attachQrDTO;
    }
}
